package web.admin.config;

import domain.Address;
import domain.MyConfiguration;
import domain.User;
import domain.shop.*;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.ConfigurationService;
import service.velocity.VelocityService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


public class MailConfigurationPanel extends Panel {
    private final static Logger logger = Logger.getLogger(MailConfigurationPanel.class.getName());
    @SpringBean
    private VelocityService velocityService;
    @SpringBean
    private ConfigurationService configurationService;

    public MailConfigurationPanel(String id) {
        super(id);
        final IModel<MyConfiguration> configModel = new LoadableDetachableModel<MyConfiguration>() {

            @Override
            protected MyConfiguration load() {
                return configurationService.getConfig();
            }
        };
        add(new Form<MyConfiguration>("form", configModel) {

            {
                setDefaultModel(new CompoundPropertyModel<MyConfiguration>(configModel));
                add(new TextField("templateDirectory").setRequired(true));
                add(new TextField("inputEncoding").setRequired(true));
                add(new TextField("outputEncoding").setRequired(true));
                add(new TextField("registrationMailSubject").setRequired(true));
                add(new TextField("orderMailSubject").setRequired(true));
                add(new TextField("passwordResetMailSubject").setRequired(true));
                add(new TextField("passwordResetRequestMailSubject").setRequired(true));
                add(new TextField("accountActivationMailSubject").setRequired(true));

            }

            @Override
            protected void onSubmit() {
                try {
                    configurationService.updateConfiguration(getModelObject());
                } catch (Exception e) {
                    logger.log(Level.SEVERE, null, e);
                    error(getLocalizer().getString("system.error", this));
                }
                info(getLocalizer().getString("saved", this));
            }
        });
        add(new TemplateForm("OrderMailTemplateForm", velocityService.getOrderMailTemplatePath()));
        add(new TemplateForm("RegistrationTemplateForm", velocityService.getRegistrationMailTemplatePath()));
        add(new TemplateForm("PasswordResetTemplateForm", velocityService.getPasswordResetTemplatePath()));
        add(new TemplateForm("PasswordResetRequestTemplateForm", velocityService.getPasswordResetRequestTemplatePath()));
        add(new TemplateForm("accountActivationMailTemplateForm", velocityService.getAccountActivationMailTemplatePath()));

        add(new MultiLineLabel("OrderMailTestPanel", new AbstractReadOnlyModel<String>() {

            @Override
            public String getObject() {
                try {
                    return velocityService.getOrderMail(getTestData());
                } catch (DirectoryPathException e) {
                    logger.log(Level.SEVERE, e.getMessage());
                    return e.getMessage();
                }
                catch (Exception ex) {
                    logger.log(Level.SEVERE, "OrderMailTestPanel", ex);
                    return ex.getMessage();
                }
            }

            Order getTestData() {
                User u = new User();
                u.setFirstName("vojta");
                u.setLastName("krasa");
                u.setLogin("krasa@doma.cz");
                final Order o = new Order();
                o.setUser(u);
                final PayMethod payMethod = new PayMethod();
                payMethod.setName("hotove");
                o.setPayMethod(payMethod);
                final DeliveryMethod dd = new DeliveryMethod();
                dd.setName("hotove");
                o.setDeliveryMethod(dd);
                final Address address = new Address();
                address.setCountry("Cechy");
                address.setStreet("Nejaka Ulice");
                address.setMunicipality("Praha");
                address.setPostcode("004");
                o.setDeliveryAddress(address);
                o.setTotalPrice(BigDecimal.valueOf(35131));
                final ArrayList<BasketItem> a = new ArrayList<BasketItem>();
                BasketItem b = new BasketItem();
                Product p = new Product("produukt");
                Product p1 = new Product("produukt1");
                b.setMultiplicity(10);
                b.setProduct(p);
                b.setPrice(BigDecimal.valueOf(12));
                BasketItem b1 = new BasketItem();
                b1.setMultiplicity(10);
                b1.setProduct(p1);
                b1.setPrice(BigDecimal.valueOf(12));
                a.add(b);
                a.add(b1);
                o.setProducts(a);
                return o;
            }
        }));

        add(new MultiLineLabel("accountActivationTestPanel", new AbstractReadOnlyModel<String>() {

            @Override
            public String getObject() {
                try {
                    return velocityService.getAccountActivationMail(getTestData());
                } catch (DirectoryPathException e) {
                    logger.log(Level.SEVERE, e.getMessage());
                    return e.getMessage();
                } catch (Exception ex) {
                    logger.log(Level.SEVERE, "accountActivationTestPanel", ex);
                    return ex.getMessage();
                }
            }

            User getTestData() {
                User u = new User();
                u.setFirstName("vojta");
                u.setLastName("krasa");
                u.setLogin("krasa@doma.cz");
                return u;
            }
        }));
        add(new MultiLineLabel("registrationTestPanel", new AbstractReadOnlyModel<String>() {

            @Override
            public String getObject() {
                try {
                    return velocityService.getRegistrationMail(getTestData());
                } catch (DirectoryPathException e) {
                    logger.log(Level.SEVERE, e.getMessage());
                    return e.getMessage();
                } catch (Exception ex) {
                    logger.log(Level.SEVERE, "registrationTestPanel", ex);
                    return ex.getMessage();
                }
            }

            User getTestData() {
                User u = new User();
                u.setFirstName("vojta");
                u.setLastName("krasa");
                u.setLogin("krasa@doma.cz");
                return u;
            }
        }));
        add(new MultiLineLabel("passwordResetTestPanel", new AbstractReadOnlyModel<String>() {

            @Override
            public String getObject() {
                try {
                    return velocityService.getPasswordResetMail(getTestData(), "nejakyNovyHeslo");
                } catch (DirectoryPathException e) {
                    logger.log(Level.SEVERE, e.getMessage());
                    return e.getMessage();
                } catch (Exception ex) {
                    logger.log(Level.SEVERE, "passwordResetTestPanel", ex);
                    return ex.getMessage();
                }
            }

            User getTestData() {
                User u = new User();
                u.setFirstName("vojta");
                u.setLastName("krasa");
                u.setLogin("krasa@doma.cz");
                return u;
            }
        }));
        add(new MultiLineLabel("passwordResetRequesTestPanel", new AbstractReadOnlyModel<String>() {

            @Override
            public String getObject() {
                try {
                    return velocityService.getPasswordResetRequestMail(getTestData(), "Nahodne-Generovanej-Klic");
                } catch (DirectoryPathException e) {
                    logger.log(Level.SEVERE, e.getMessage());
                    return e.getMessage();
                } catch (Exception ex) {
                    logger.log(Level.SEVERE, "passwordResetRequesTestPanel", ex);
                    return ex.getMessage();
                }
            }

            User getTestData() {
                User u = new User();
                u.setFirstName("vojta");
                u.setLastName("krasa");
                u.setLogin("krasa@doma.cz");
                return u;
            }
        }));
    }


}
